package com.yjt.auth.common.util.session.manager;

import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpSession;

public class HttpSessionManager {
    private static final ConcurrentHashMap<String, HttpSession> sessionsMap = new ConcurrentHashMap<String, HttpSession>();
    private HttpSessionManager(){}
 
    /**
     * session创建的时候把一个HttpSession加入HttpSessionManager
     */
    public static boolean addSession(HttpSession session) {
        if (session == null) {
           return false;
        }
        if (sessionsMap.containsKey(session.getId())) {
            return false;
        } else {
            sessionsMap.putIfAbsent(session.getId(), session);
            return true;
        }
    }
 
    /**
     * session创建销毁的时候把一个HttpSession从HttpSessionManager删除
     */
    public static boolean removeSession(HttpSession session) {
        if (session == null) {
            return false;
        }
        synchronized(sessionsMap) {
	        if (sessionsMap.containsKey(session.getId())) {
	            sessionsMap.remove(session.getId());
	            return true;
	        } else {
	        	return false;
	        }
        }
    }
}